package com.practice.order.service.Impl;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.practice.order.feign.ProductFeignClient;
import com.practice.order.order.bean.Order;
import com.practice.order.product.Product;
import com.practice.order.service.OrderService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private ProductFeignClient productFeignClient;

    @SentinelResource(value = "createOrder",blockHandler = "createOrderFallback")
    @Override
    public Order createOrder(Long productId , Long userId) {
        //调用远程获取商品的方法
//        Product product = getProductFromRemoteWithLoadBalancerClient(productId);
//        Product product = getProductFromRemoteWithLoadBalancerClientWithAnnotation(productId);
        //调用feign获取商品信息
        Product product = productFeignClient.getProduct(productId);
        Order order = new Order();
        order.setId(1L);
        order.setUserId(userId);
        order.setNickName("zhangsan");
        order.setAddress("尚硅谷");
        order.setProductList(Arrays.asList(product));
        // 优化后的总金额计算
        BigDecimal totalAmount = product.getPrice().multiply(BigDecimal.valueOf(product.getNum()));
        order.setTotalAmount(totalAmount);

        System.out.println(product.toString());

        return order;
    }

    //执行兜底回调
    public Order createOrderFallback(Long productId , Long userId, BlockException  ex) {
        Order order = new Order();
        order.setId(0L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("未知用户");
        order.setAddress("异常信息："+ex.getMessage());

        return order;
    }

    /**
     * 法一：简单远程调用
     * 获取商品信息
     * @param productId
     * @return
     */
/*    private Product getProductFromRemote(Long productId) {
        //1.获取到商品服务所在的所有机器Ip+port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        //得到第一个实例对象
        ServiceInstance serviceInstance = instances.get(0);

        //远程URL
        String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/product/"+productId;

        //2.给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);

        return product;
    }*/

    /**
     * 法二：用 loadBalancerClient
     * 获取商品信息
     * @param productId
     * @return
     */
/*    private Product getProductFromRemoteWithLoadBalancerClient(Long productId) {
        //1.获取到商品服务所在的所有机器Ip+port
        ServiceInstance choose = loadBalancerClient.choose("service-product");

        //远程URL
        String url = "http://"+choose.getHost()+":"+choose.getPort()+"/product/"+productId;
        System.out.println( "url : "+url);
        //2.给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);

        return product;
    }*/

    /**
     * 法三：用 @LoadBalanced 基于注解的负载均衡
     * 获取商品信息
     * @param productId
     * @return
     */
    private Product getProductFromRemoteWithLoadBalancerClientWithAnnotation(Long productId) {

        //远程URL
        String url = "http://service-product/product/"+productId;
        //2.给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);

        return product;
    }

}
